import url from "url";
import mime from "mime";
import fs from "fs";
export default function (req, res, next) {
  // 扩展渲染方法

  req.path = url.parse(req.url).pathname;
  req.query = url.parse(req.url, true).query;
  //   ....

  res.send = function (data) {
    if (typeof data === "string") {
      res.setHeader("Content-Type", "text/palin");
      res.end(data);
    } else if (typeof data === "object") {
      res.setHeader("Content-Type", "application/json");
      res.end(JSON.stringify(data));
    } else {
      res.end("not found");
    }
    // ....
  };
  res.sendFile = function (file, { root } = {}) {
    if (root) {
      file = path.resolve(root, file);
    }
    res.setHeader("Content-Type", mime.lookup(file) + ";charset=utf-8");
    fs.createReadStream(file).pipe(res);
  };

  res.render = function (filename, data) {
    res.app.render(filename, data, (template) => {
      res.end(template);
    });
  };
  next();
}

// 静态服务 ， req.body
